package com.yunji.order.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunji.common.annotation.DataScope;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.order.bo.GetOrderTradeBO;
import com.yunji.order.dao.ITradeDao;
import com.yunji.order.domain.TradeDO;
import com.yunji.order.dto.TradeParam;
import com.yunji.order.mapper.ITradeMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2024/12/16 15:29
 * @description : TODO
 **/
@Repository
public class TradeDaoImpl extends BaseDaoImpl<ITradeMapper, TradeDO> implements ITradeDao {


    @Override
    public TradeDO getTrade(GetOrderTradeBO bo) {
        return lambdaQuery().eq(TradeDO::getTransactionNo, bo.getTransactionNo()).eq(TradeDO::getTradeType, bo.getTradeType()).one();
    }

    @Override
    public boolean existsTrade(GetOrderTradeBO bo) {
        return lambdaQuery().eq(TradeDO::getTransactionNo, bo.getTransactionNo()).eq(TradeDO::getTradeType, bo.getTradeType()).exists();
    }

    @Override
    public TradeDO getByTradeCode(String tradeCode) {
        return lambdaQuery().eq(TradeDO::getTransactionNo, tradeCode).one();
    }

    @Override
    public List<TradeDO> listByTranNo(String transactionNo) {
        return lambdaQuery().eq(TradeDO::getTransactionNo, transactionNo).list();
    }

    @DataScope
    @Override
    public List<TradeDO> tradeList(TradeParam param) {
        LambdaQueryWrapper<TradeDO> query = new LambdaQueryWrapper<>();
        if (param.getCreateTimeE() != null)
            param.setCreateTimeE(param.getCreateTimeE().plusDays(1L));
        query.in(!CollectionUtils.isEmpty(param.getAccountTypeList()), TradeDO::getAccountType, param.getAccountTypeList())
                .in(!CollectionUtils.isEmpty(param.getTradeModeList()), TradeDO::getTradeMode, param.getTradeModeList())
                .in(!CollectionUtils.isEmpty(param.getPayModeList()), TradeDO::getPayMode, param.getPayModeList())
                .like(StringUtils.hasLength(param.getAccountName()), TradeDO::getAccountName, param.getAccountName())
                .like(StringUtils.hasLength(param.getAccountTel()), TradeDO::getAccountTel, param.getAccountTel())
                .eq(StringUtils.hasLength(param.getApiCode()), TradeDO::getApiCode, param.getApiCode())
                .like(StringUtils.hasLength(param.getTransactionNo()), TradeDO::getTransactionNo, param.getTransactionNo())
                .eq(param.getPayType() != null, TradeDO::getPayType, param.getPayType())
                .ge(param.getCreateTimeS() != null, TradeDO::getCreateTime, param.getCreateTimeS())
                .le(param.getCreateTimeE() != null, TradeDO::getCreateTime, param.getCreateTimeE())
                .orderByDesc(TradeDO::getCreateTime);
        if (param.isExport())
            return list(query);
        return pageList(param, query);
    }
}
